package org.kordakus.shoot.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableModel;

import net.miginfocom.swing.MigLayout;

import org.apache.log4j.Logger;
import org.kordakus.shoot.ShootController;
import org.kordakus.shoot.gui.action.AddFileAction;
import org.kordakus.shoot.gui.action.RemoveFileAction;
import org.kordakus.shoot.gui.renderer.CameraMakeTableCellRenderer;
import org.kordakus.shoot.gui.renderer.CameraModelTableCellRenderer;
import org.kordakus.shoot.gui.renderer.DateTakenTableCellRenderer;
import org.kordakus.shoot.gui.renderer.FileNameTableCellRenderer;
import org.kordakus.shoot.gui.renderer.NewDateTakenTableCellRenderer;

public class ShootFrame extends JFrame implements WindowListener
{

	private static final long		serialVersionUID	= -2318736858063893652L;

	private static final Logger		_logger				= Logger.getLogger(ShootFrame.class);

	private final ShootController	_controller;

	private TableModel				_pictureTableModel;
	private JTable					_pictureTable;

	public ShootFrame()
	{
		this._controller = new ShootController();
		initializeComponent();
	}

	public ShootFrame(ShootController controller)
	{
		this._controller = controller;
		initializeComponent();
	}

	private void initializeComponent()
	{
		_logger.info("Initializing shoot frame...");

		this.setLayout(new MigLayout("ins 0", "[grow,fill]", "[grow,fill]0[]"));

		this.add(createBodyPanel(), "wrap");
		this.add(createFootPanel(), "wrap");

		this.setTitle("JShoot!");
		this.setMinimumSize(new Dimension(ShootGuiUtils.WINDOW_WIDTH, ShootGuiUtils.WINDOW_HEIGH));
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setLocationByPlatform(true);
		this.setVisible(true);

		new ImportProgressDialog(this);
	}

	private JPanel createBodyPanel()
	{
		JPanel tablePanel = createTablePanel();

		JPanel bodyPanel = new JPanel(new MigLayout("ins 10", "[grow,fill]", "[grow,fill]"));
		bodyPanel.setOpaque(true);
		bodyPanel.setBackground(Color.WHITE);

		bodyPanel.add(tablePanel);

		return bodyPanel;
	}

	private JPanel createTablePanel()
	{
		JPanel tablePanel = new JPanel(new MigLayout("ins 10", "[grow,fill]", "[grow,fill]10[]"));
		tablePanel.setOpaque(true);
		tablePanel.setBackground(Color.WHITE);
		tablePanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY), "Images"));

		_pictureTableModel = new ImageTableModel(_controller.getContext());
		_pictureTable = new JTable(_pictureTableModel);
		_pictureTable.setFillsViewportHeight(true);
		_pictureTable.createDefaultColumnsFromModel();
		_pictureTable.getColumnModel().getColumn(ShootGuiUtils.TABLE_FILENAME_INDEX)
				.setCellRenderer(new FileNameTableCellRenderer());
		_pictureTable.getColumnModel().getColumn(ShootGuiUtils.TABLE_CAMERAMAKE_INDEX)
				.setCellRenderer(new CameraMakeTableCellRenderer());
		_pictureTable.getColumnModel().getColumn(ShootGuiUtils.TABLE_CAMERAMODEL_INDEX)
				.setCellRenderer(new CameraModelTableCellRenderer());
		_pictureTable.getColumnModel().getColumn(ShootGuiUtils.TABLE_DATETIMEORIGINAL_INDEX)
				.setCellRenderer(new DateTakenTableCellRenderer());
		_pictureTable.getColumnModel().getColumn(ShootGuiUtils.TABLE_NEWDATETIMEORIGINAL_INDEX)
				.setCellRenderer(new NewDateTakenTableCellRenderer());
		_pictureTable.setRowHeight(22);
		_pictureTable.setIntercellSpacing(new Dimension(0, 0));
		_pictureTable.setShowGrid(false);
		_pictureTable.setAutoCreateRowSorter(true);
		_pictureTable.setFont(ShootGuiUtils.FONT_FORM);

		JScrollPane pictureTableScrollPane = new JScrollPane(_pictureTable);
		pictureTableScrollPane.setBorder(BorderFactory.createLineBorder(Color.GRAY));

		JButton addFileButton = new JButton(new AddFileAction(this, _controller));
		addFileButton.setMaximumSize(ShootGuiUtils.DIMENSION_BUTTON_SIZE);
		addFileButton.setMinimumSize(ShootGuiUtils.DIMENSION_BUTTON_SIZE);
		addFileButton.setFont(ShootGuiUtils.FONT_FORM);
		JButton removeFileButton = new JButton(new RemoveFileAction(this, _controller));
		removeFileButton.setMaximumSize(ShootGuiUtils.DIMENSION_BUTTON_SIZE);
		removeFileButton.setMinimumSize(ShootGuiUtils.DIMENSION_BUTTON_SIZE);
		removeFileButton.setFont(ShootGuiUtils.FONT_FORM);

		JPanel tableButtonPanel = new JPanel(new MigLayout("ins 0", "[grow,fill][align right][align right]", "[]"));
		tableButtonPanel.setBorder(BorderFactory.createEmptyBorder());
		tableButtonPanel.setOpaque(true);
		tableButtonPanel.setBackground(Color.WHITE);
		tableButtonPanel.add(new JLabel());
		tableButtonPanel.add(addFileButton);
		tableButtonPanel.add(removeFileButton);

		tablePanel.add(pictureTableScrollPane, "wrap");
		tablePanel.add(tableButtonPanel);

		return tablePanel;
	}

	private JPanel createFootPanel()
	{
		JPanel footPanel = new JPanel();
		footPanel.setOpaque(true);
		footPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.GRAY),
				BorderFactory.createMatteBorder(1, 0, 0, 0, Color.WHITE)));
		footPanel.setMinimumSize(new Dimension(0, 60));
		return footPanel;
	}

	//
	//
	// Window Listener interface

	@Override
	public void windowOpened(WindowEvent e)
	{
	}

	@Override
	public void windowClosing(WindowEvent e)
	{
	}

	@Override
	public void windowClosed(WindowEvent e)
	{
	}

	@Override
	public void windowIconified(WindowEvent e)
	{
	}

	@Override
	public void windowDeiconified(WindowEvent e)
	{
	}

	@Override
	public void windowActivated(WindowEvent e)
	{
	}

	@Override
	public void windowDeactivated(WindowEvent e)
	{
	}

}
